//
//  XJHomeStatusCell.swift
//  新浪微博
//
//  Created by 徐绩 on 16/4/20.
//  Copyright © 2016年 ayumi. All rights reserved.
//

import UIKit
import SnapKit

let bottomBarH: CGFloat = 35

class XJHomeStatusCell: UITableViewCell {
    
    var bottomBarTopContrains: Constraint?
    
    var statusViewModel: XJHomeCellViewModel? {
        
        didSet{
            originalView.statusModel = statusViewModel
            
                //底部视图的顶部约束相对的参照物不一样,不能直接更新约束,先让其失效,再重新赋值
            bottomBarTopContrains?.uninstall()
            if statusViewModel?.status?.retweeted_status != nil {
            
                //转发微博赋值
                retweetView.homeCellViewModel = statusViewModel
                retweetView.hidden = false
                bottomBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                  bottomBarTopContrains = make.top.equalTo(retweetView.snp_bottom).constraint
                    
                })
            
            }else{
            
                retweetView.hidden = true
                bottomBar.snp_makeConstraints(closure: { (make) -> Void in
                   bottomBarTopContrains = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /**
     布局cell界面
     */
    private func setupUI() {
        contentView.backgroundColor = UIColor(white: 250/255, alpha: 1)
        selectionStyle = .None
        //添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(bottomBar)
        contentView.addSubview(retweetView)
        
        //原创视图
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(10)

        }
        //底部视图
        bottomBar.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(originalView)
            bottomBarTopContrains = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(bottomBarH)
            
        }
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.leading.trailing.equalTo(self)
            make.bottom.equalTo(bottomBar.snp_bottom)

        }
        //转发视图
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(originalView)
            make.top.equalTo(originalView.snp_bottom)

            
        }
        
    }
    
    /// 懒加载cell上的控件
    //原创微博
   private lazy var originalView: XJOriginalView = XJOriginalView()
    
    //底部视图
    private lazy var bottomBar: XJStatusToolBar = XJStatusToolBar()
    
    //转发微博
    private lazy var retweetView: XJRetweetView = XJRetweetView()
    
}
